//
//  CZVistorView.swift
//  SinaSwift
//
//  Created by I Do Believe on 15/11/24.
//  Copyright © 2015年 apple11. All rights reserved.
//

import UIKit
/*
frame:
origin: 位置 (相对父控件)
size: 设置大小 (写死的数值)

autolayout:
位置: 参照比较丰富
大小: 参照比较丰富
*/

//协议
protocol CZVistorViewDelegate : NSObjectProtocol {
    //登录按钮点击    默认必选
    func vistorViewLoginClick( )
}
//访客视图
class CZVistorView: UIView {

    //MARK: - 属性
    ///代理 weak
    weak var delegate : CZVistorViewDelegate?
    
    //swift默认所有的view都能通过xib/storyboard加载
     required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     override init(frame: CGRect) {
        //super 是要在普通的存储型属性的下面
        super.init(frame: frame)
    
        //设置背景色 //当r,g,b3个值都一样时就可以使用white方法
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI( )
        }
    
    //MARK: - 按钮点击事件
    func loginClick( ) {
//        print(__FUNCTION__)
        
        //modal另外一个控制器（只有控制器有这个功能）
        //通知代理
        delegate?.vistorViewLoginClick( )
    }
    
    //测试动画在跳转时是否移除
//        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//            let a = iconview.layer.animationForKey("rotation")
//            print("动画 a:\(a)")
//        }
    
        //MARK: - 转轮动画
        func rotationAnimation( ) {
            //核心动画
            let rotation = CABasicAnimation(keyPath: "transform.rotation")
            
            //设置动画参数
            rotation.toValue = 2 * M_PI
            rotation.duration = 20
            rotation.repeatCount = MAXFLOAT
            // 当不可见的时候,系统会认为动画完成,在完成的时候不要移除动画
            rotation.removedOnCompletion = false
            
        //开始核心动画
            iconview.layer.addAnimation(rotation, forKey: "rotation")
    }
        //MARK: - 核心动画开始和暂停
            ///暂停旋转
            func pauseAnimation() {
                // 记录暂停时间
                let pauseTime = iconview.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
                
                // 设置动画速度为0
                iconview.layer.speed = 0
                
                // 设置动画偏移时间
                iconview.layer.timeOffset = pauseTime
            }
            /// 恢复旋转
            func resumeAnimation() {
                // 获取暂停时间
                let pauseTime = iconview.layer.timeOffset
                
                // 设置动画速度为1
                iconview.layer.speed = 1
                
                // 重置偏移时间
                iconview.layer.timeOffset = 0
                
                // 重置开始时间
                iconview.layer.beginTime = 0
                
                // 计算开始时间
                let timeSincePause = iconview.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
                
                // 设置开始时间
                iconview.layer.beginTime = timeSincePause
            }
  
    
    //MARK: - 设置访客视图的内容
    /**
    *设置访客视图的内容
    *
    *  @param imageName:图片名称
    *  @param message:内容
    */
    func setupVistorView(imageName: String, message: String) {
        //图片 替换转轮的图片
        iconview.image = UIImage(named: imageName)
        //文字    替换内容
        msgLabel.text = message
        //适应大小
        msgLabel.sizeToFit( )
        //隐藏小房子
        homeView.hidden = true
    }
    
    ///准备UI
    private func prepareUI( ) {
        //添加子控件 注意添加顺序
        addSubview(iconview)
        //添加遮罩
        addSubview(coverView)//必须添加在这里
        
        addSubview(homeView)
        addSubview(msgLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //添加约束 autoLayout   注意：要设置约束的view有父控件后才能设置他的自动布局约束
        //必须关闭autoresizing，不让它干扰autolayout的约束
        iconview.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        msgLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //创建约束对象    约束需要添加到view上面  可以统一添加到父控件上面(不清楚添加到自身还是父控件上面时)
        //iconview center.x 与父控件的center.x重合
        /// iconView CenterX与父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 设置的约束
        // relatedBy: Equal
        // toItem: 参照的view
        // attribute: 参照的view的约束
            addConstraint( NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        /// iconView CenterY与父控件的CenterY重合
        addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //小房子的约束  -->参照转轮
       //x
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
       //y
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息label
        //x
        addConstraint( NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
       // y   顶部距离label底部16
        addConstraint( NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
      //宽度约束    226是随便给的
        // 当 toItem = nil时attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint( NSLayoutConstraint(item: msgLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
      
        
        // 注册按钮
        // 左边和label对齐
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        // 高
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右边和label对齐
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: msgLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        // 高
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮罩约束
        //左边
       addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //右边
        addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //上边
        addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //下边
        addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    }
    
    //MARK: - 懒加载
    ///转轮
    private lazy var iconview: UIImageView = UIImageView( image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    ///小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///消息label
    private lazy var msgLabel : UILabel = {
        let label = UILabel( )
        
        label.textColor = UIColor.darkGrayColor( )
        //设置label文本
        label.text = "关注一些人，看看有什么惊喜"
        label.numberOfLines = 0
        //根据文本来适应大小
        label.sizeToFit( )
        return label
    }( )
    ///注册按钮
    private lazy var registerBtn : UIButton = {
        let button = UIButton ( )
    
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置文本颜色和大小
        button.setTitleColor(UIColor.orangeColor( ), forState: UIControlState.Normal)
       button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //根据内容适应大小
        button.sizeToFit( )
        return button
    }( )

    ///登录按钮
    private lazy var loginBtn : UIButton = {
        let button = UIButton ( )
        
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置文本
        button.setTitle("登录", forState: UIControlState.Normal)
        
        //设置文本颜色和大小
        button.setTitleColor(UIColor.blackColor( ), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //根据内容适应大小
        button.sizeToFit( )
        //添加点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }( )
    
    //遮罩视图        在转轮和房子之间放置遮罩
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
